package base.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Thread pool factory
 *
 * @description: demo建议线程池全部由spring容器维护，便于后期项目业务激增优化调整
 *
 * @author: cyj
 * @date 2020 -11-11 10:52:04
 */
@Slf4j
@Configuration
@EnableAsync
public class ThreadPoolFactory {

    /**
     * Pool size
     *
     * @description:
     * @author: cyj
     * @date 2020 -11-11 10:52:04
     */
    enum PoolSize {
        /**
         * Single pool size
         */
        SINGLE(1),
        /**
         * Tiny pool size
         */
        TINY(3),
        /**
         * Small pool size
         */
        SMALL(5),
        /**
         * Medium pool size
         */
        MEDIUM(8),
        /**
         * Large pool size
         */
        LARGE(15);
        /**
         * Size
         */
        public int size;

        /**
         * Pool size
         *
         * @param size size
         */
        PoolSize(int size) {
            this.size = size;
        }
    }

    /**
     * Queue capacity
     *
     * @description:
     * @author: cyj
     * @date 2020 -11-11 10:52:04
     */
    enum QueueCapacity {
        /**
         * Single queue capacity
         */
        SINGLE(1),
        /**
         * Tiny queue capacity
         */
        TINY(100),
        /**
         * Small queue capacity
         */
        SMALL(500),
        /**
         * Medium queue capacity
         */
        MEDIUM(1000),
        /**
         * Large queue capacity
         */
        LARGE(3000),
        /**
         * Very large queue capacity
         */
        VERY_LARGE(5000),
        /**
         * Max queue capacity
         */
        MAX(Integer.MAX_VALUE);
        /**
         * Size
         */
        public int size;

        /**
         * Queue capacity
         *
         * @param size size
         */
        QueueCapacity(int size) {
            this.size = size;
        }
    }

    /**
     * Insignificant issue executor
     *
     * @return the executor
     * @description: 处理无关紧要的操作, 对于溢出的线程直接丢弃
     */
    @Bean("insignificantIssue")
    @Lazy
    public ThreadPoolTaskExecutor insignificantIssue() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程大小
        taskExecutor.setCorePoolSize(PoolSize.TINY.size);
        // 最大线程大小
        taskExecutor.setMaxPoolSize(PoolSize.TINY.size);
        // 阻塞队列大小
        taskExecutor.setQueueCapacity(QueueCapacity.SMALL.size);
        // 空闲线程存活时间
        taskExecutor.setKeepAliveSeconds(60);
        // 线程名前缀
        taskExecutor.setThreadNamePrefix("insignificantIssue-");
        // 线程拒绝策略(此线程池操作无关紧要的动作,可以不记录)
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        taskExecutor.initialize();
        return taskExecutor;
    }

}
